<?
require_once('DataObject/DomainModel.php');

class OrdersStatusesChanges extends DomainModel {
	protected $id;	//primaryKey=yes, dataType=int, maxLength=, allowNulls=no
	protected $ordersId;	//primaryKey=no, dataType=int, maxLength=, allowNulls=no
	protected $statusesId;	//primaryKey=no, dataType=smallint, maxLength=, allowNulls=no
	protected $addDate;	//primaryKey=no, dataType=timestamp, maxLength=, allowNulls=no
	protected $usersId;	//primaryKey=no, dataType=int, maxLength=, allowNulls=yes
	protected $remarks;	//primaryKey=no, dataType=varchar, maxLength=500, allowNulls=yes
	
	
}

class OrdersStatusesChangesMapper extends ModelMapper {
	function __construct(){
		parent::__construct();
		$this->tableName = 'orders_statuses_changes';
		$this->selectViewName = 'v_orders_statuses_changes';
		$this->selectFields = '
				id,
				orders_id,
				statuses_id,
				add_date,
				users_id,
				remarks
		';

		$this->findQuery = self::$DB->prepare("
			SELECT ".$this->selectFields."
			FROM ".$this->selectViewName."
			WHERE id = ?
		");
		$this->insertQuery = self::$DB->prepare("
			CALL p_add_orders_statuses_changes (@p_id,
				?,
				?,
				?,
				?,
				?
		)");


		$this->updateQuery = self::$DB->prepare("
			CALL p_upd_orders_statuses_changes (
				?,
				?,
				?,
				?,
				?,
				?

		)");

		$this->deleteQuery = self::$DB->prepare("
			CALL p_del_orders_statuses_changes (?)
		");

		$this->cacheTime = null;
	}

	protected function doLoad($array){
		$acuire = str_replace('Mapper', '', get_class($this));
		$obj = new $acuire($array['id']);
		$obj->setId($array['id']);
		$obj->setOrdersId($array['orders_id']);
		$obj->setStatusesId($array['statuses_id']);
		$obj->setAddDate($array['add_date']);
		$obj->setUsersId($array['users_id']);
		$obj->setRemarks($array['remarks']);

		return $obj;
	}

	function insert(DomainModel $obj){

		$values = array(
			$obj->getOrdersId(),
			$obj->getStatusesId(),
			$obj->getAddDate(),
			$obj->getUsersId(),
			$obj->getRemarks()
		);

		$this->doStatement($this->insertQuery, $values);
		$obj->setId(self::$DB->getOne("SELECT @p_id"));
	}

	function update(DomainModel $obj){
		$values = array(
			$obj->getId(),
			$obj->getOrdersId(),
			$obj->getStatusesId(),
			$obj->getAddDate(),
			$obj->getUsersId(),
			$obj->getRemarks()
		);

		$this->doStatement($this->updateQuery, $values);
	}

	function delete(DomainModel $obj){
		$this->doStatement($this->deleteQuery, array($obj->getId()));
	}

	function newId(){

		$row = self::$DB->getRow('SHOW TABLE STATUS LIKE orders_statuses_changes');
		return $row['Auto_increment'];

	}
	
	function findByOrdersId($ordersId){
		$query = self::$DB->prepare("
			SELECT 
			    osc.id, 
			    osc.orders_id, 
			    osc.statuses_id, 
			    osc.add_date, 
			    osc.users_id, 
			    osc.remarks,
			    vs.status
			FROM v_statuses vs
			LEFT JOIN orders_statuses_changes osc ON vs.id = osc.statuses_id AND (osc.orders_id = '".$ordersId."')
			WHERE vs.sc_catalogue = 'orders' 
			AND vs.status != 'rejected'
			ORDER BY vs.id ASC
			"
		);
		
		return self::$DB->getAll($query);
	}
}

class OrdersStatusesChangesCollection extends ModelCollection {
	function add(OrdersStatusesChanges $model){
		$this->doAdd($model);
	}
}

class OrdersStatusesChangesCollectionExtended extends OrdersStatusesChangesCollection {
	private $stmt;
	private $valueArray;
	private $mapper;
	private $run = false;

	function __construct(ModelMapper $mapper, $stmtHandle, $valueArray){
		parent::__construct();
		$this->stmt = $stmtHandle;
		$this->valueArray = $valueArray;
		$this->mapper = $mapper;
	}

	function notifyAccess(){
		if (!$this->run){
			$result = $this->mapper->doStatement($this->stmt, $this->valueArray,$this->mapper->getCacheTime());
			$this->init_db($result, $this->mapper);
			$this->run = true;
		}
	}
}

?>